package com.mobinters.cafeman.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.mobinters.cafeman.module.MainSlideModule;
import com.mobinters.cafeman.object.Product;
import com.mobinters.image.ImageLoader;
import com.mobinters.image.Utils;
import com.mobinters.cafeman.DetailActivity;
import com.mobinters.cafeman.R;
public class ProductAdapter extends BaseAdapter {

	Context mContext;
	LayoutInflater mInflater;
	List<Product> mData;
	List<Product> mFullData;
	//public List<ItemMeal> CheckedList;
	long MEMORY_MAX;
	ImageLoader imageLoader;
	Bitmap mDefaultBitmap;
	int IconDimension;
	
	

	public ProductAdapter(Context context,List<Product> data){
		mContext=context;
		mFullData=data;
		mData = new ArrayList<Product>();
		mData.addAll(mFullData);
		
		mInflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		MEMORY_MAX=mContext.getResources().getInteger(R.integer.icon_memory_max)*1024*1024;
		IconDimension=mContext.getResources().getInteger(R.integer.icon);
		mDefaultBitmap=Utils.createBitmap(Color.GRAY, IconDimension, IconDimension);
		imageLoader=new ImageLoader(mContext, MEMORY_MAX, mDefaultBitmap,IconDimension);
		
		
		
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int i) {
		// TODO Auto-generated method stub
		return i;
	}

	@Override
	public long getItemId(int i) {
		// TODO Auto-generated method stub
		return i;
	}

	@Override
	public View getView(final int i, View view, ViewGroup viewgroup) {
		// TODO Auto-generated method stub
		if(view==null){
			view=mInflater.inflate(R.layout.main_listview_item, viewgroup, false);
		}
		Product item = mData.get(i);
		TextView tv=(TextView)view.findViewById(R.id.txt_title);
		tv.setText(item.ProductName1);
		
		tv=(TextView)view.findViewById(R.id.txt_description);
		if(item.Description!=null) tv.setText(item.Description);
		
		ImageView iv=(ImageView)view.findViewById(R.id.iv_icon);
		imageLoader.DisplayImage(item.IconUrl, iv);
		
		
		
		/*
		 * FOR CLICK LISTENER
		 */
		final CheckBox cb = (CheckBox) view.findViewById(R.id.cb_confirm);
		cb.setChecked(item.Checked);
		
		cb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				
				int id = mData.get(i).ProductId;
				Product item = findItemById(id);
				if(item !=null) item.Checked = cb.isChecked();
				
				//Log.e("click","checkbox " + "position "+i + "- id "+ id);
				//update the listview in slide
				if(MainSlideModule.OrderAdapter!= null && MainSlideModule.OrderAdapter.isCheckedList()) 
					MainSlideModule.OrderAdapter.notifyDataSetChanged();
			}
		});
		
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(mContext, DetailActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				//intent.putExtra("content", mData.get(i).ContentUrl);
				//intent.putExtra("title", mData.get(i).Title);
				//mContext.startActivity(intent);
			}
		});
		
		
		Spinner spinNumber = (Spinner) view.findViewById(R.id.spin_number);
		spinNumber.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long arg3) {
				// TODO Auto-generated method stub
				int id = mData.get(i).ProductId;
				Product item = findItemById(id);
				if(item !=null) {
					String value = parent.getItemAtPosition(position).toString();
					item.Number = Integer.parseInt(value);
					//if(MainSlideModule.OrderAdapter.isCheckedList()) MainSlideModule.OrderAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		return view;
	}

	
	private Product findItemById(int id){
		for(Product item: mFullData){
			if(item.ProductId == id) return item;
		}
		return null;
	}
	/*
	 * return true if the number of row > 0; false if the number =0 <--> No found
	 */
	@SuppressLint("DefaultLocale")
	public boolean filter(String searchText){
		
		if(searchText==null || searchText.length() == 0){
			//display full lisview
			
			mData = new ArrayList<Product>();
			mData.addAll(mFullData);
			notifyDataSetChanged();
			return true;
			
		}
		searchText = searchText.toLowerCase();
		List<Product> temList = new ArrayList<Product>();
		for(Product item: mFullData){
			if(item.ProductName1.toLowerCase().contains(searchText)){
				temList.add(item);
			}
		}
		
		if(temList.size()==0){
			return false;
		}
		
		mData = new ArrayList<Product>();
		mData.addAll(temList);
		notifyDataSetChanged();
		return true;
	}
	
}
